class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        srand(time(NULL));
        int ret = QuickSort(nums, 0, nums.size() - 1, k);
        return ret;
    }

    int QuickSort(vector<int>& nums, int begin, int end, int k)
    {
        if (begin == end)
            return nums[begin];

        int left = begin - 1, right = end + 1, i = begin;
        int key = nums[rand() % (end - begin + 1) + begin];
        while (i < right)
        {
            if (nums[i] < key)
                swap(nums[i++], nums[++left]);
            else if (nums[i] == key)
                i++;
            else
                swap(nums[i], nums[--right]);
        }
        int c = end - right + 1, b = right - left - 1;
        if (c >= k)
            return QuickSort(nums, right, end, k);
        else if (b + c >= k)
            return key;
        else
            return QuickSort(nums, begin, left, k - b - c);
    }
};